/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MODEL.DAO;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import MODEL.POJO.*;
import java.util.List;

/**
 *
 * @author samph
 */
public class WarehousesDao {

    private static int last_insert_id;

    public static Warehouses LayDeThiTheoMa(int ma) {
        Warehouses dethi = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            dethi = (Warehouses) session.get(Warehouses.class, ma);
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            //session.close();
        }
        return dethi;
    }

    public static Warehouses getIDExamFromLevel(int level, int number) {
        Warehouses dethi = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select wh from Warehouses wh where wh.customizequizs.customizeQuizId =:level and wh.warehouseNumber =:number";
            Query query = session.createQuery(hql);
            query.setParameter("level", level);
            query.setParameter("number", number);
            dethi = (Warehouses)query.list().get(0);
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            //session.close();
        }
        return dethi;
    }

    public static boolean addWarehouse(Warehouses u) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(u);
            session.flush();
            last_insert_id = u.getWarehouseId();
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex.getMessages());
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }

    public static int getMaxNumber(int level) {
        int max = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select MAX(warehouseNumber) from Warehouses wh where wh.customizequizs.customizeQuizId =:level";
            Query query = session.createQuery(hql);
            query.setParameter("level", level);
            max = Integer.parseInt(query.uniqueResult().toString());
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return max;
    }

    /**
     * @return the last_insert_id
     */
    public static int getLast_insert_id() {
        return last_insert_id;
    }

    /**
     * @param aLast_insert_id the last_insert_id to set
     */
    public static void setLast_insert_id(int aLast_insert_id) {
        last_insert_id = aLast_insert_id;
    }
}
